//
//  HOTFinePwdViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-16.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTFindPwdViewController.h"
#import "HOTResetPwdConfirmViewController.h"
#import "UITextField+extend.h"

@interface HOTFindPwdViewController () <UITextFieldDelegate>

@end

@implementation HOTFindPwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNaviTitle:@"パスワード再発行"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)requestUpdate {
    
    if (![self checkParametersValid]) {
        
        //参数检查
        return;
    }
    
    [self showLoading];
    NSDictionary *parameters = @{REQ_KEY_USER_EMAIL:_mailAddrText.text};
    
    __unsafe_unretained HOTFindPwdViewController *tempObj = self;
    [self requestWithURL:HOT_URL_RESET_PWD Parameters:parameters success:^(NSInteger result, NSDictionary * data) {
        
        
        [tempObj showSuccessWithMessage:MSG_SUCCESS complection:^{
            
            HOTResetPwdConfirmViewController *rpcvc = [[HOTResetPwdConfirmViewController alloc] initWithNibName:@"HOTResetPwdConfirmViewController" bundle:nil];
            
            [tempObj.navigationController pushViewController:rpcvc animated:YES];
        }];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

-(BOOL)checkParametersValid {
    
    NSString *errName = @"";
    HOTInputItemType inputType = 0;
    if ([_mailAddrText textIsNullOrEmpty]) {
        
        errName = @"メールアドレス";
        inputType = HOTInputItemText;
    }
    
    if (errName.length > 0) {
        
        NSString * errMsg = [self getErrMsgWith:errName itemType:inputType];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:errMsg delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        
        return NO;
    }
    
    return YES;
}

- (IBAction)sendBtnClicked:(id)sender {
    
    [self requestUpdate];
}

#pragma mark - UITextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}
@end
